﻿using System.Collections;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Input;
using Client.Processes;
using Client.ViewModels;
using Contracts.Entities;
using Microsoft.Windows.Controls;

namespace Client.Views
{
    /// <summary>
    /// Interaktionslogik für MovieListView.xaml
    /// </summary>
    public partial class MovieListView : BusyIndicator
    {
        private ReadMovie load;

        public MovieListView()
        {
            InitializeComponent();

            load = new ReadMovie();
            load.OnCompleted += m => MainViewModel.Default.AddMovieDetailsWorkspace(m, false);
        }

        public WorkspaceViewModel ViewModel
        { 
            get 
            {
                return (DataContext as WorkspaceViewModel);
            }
        }

        /// <summary>
        /// kein SelectedItems Property für Xaml
        /// http://stackoverflow.com/questions/1579700/wpf-toolkit-datagrid-multi-select-how-to-get-selecteditems-out
        /// </summary>
        private void movieListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var list = new List<MovieItem>();

            foreach (var item in movieListView.SelectedItems as IList)
            {
                if (item as MovieItem != null)
                    list.Add(item as MovieItem);
            }

            if (ViewModel != null)
                ViewModel.SelectedItems = list;   
        }

        private void movieListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var item = movieListView.SelectedItem as MovieItem;

            if (item != null)
            {
                load.Process(item.Id);
                e.Handled = true;
            }
        }
    }
}
